package com.snow.useviewdemo.function2.wait

import android.content.ClipData
import android.content.ClipboardManager
import android.os.Bundle
import com.snow.useviewdemo.R
import com.snow.useviewdemo.base.BaseKotlinActivity
import com.snow.useviewdemo.util.CountTimeUtils
import com.snow.useviewdemo.util.LogUtils
import com.snow.useviewdemo.util.ThreadCacheTools
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.act_countdownlatch_test.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.CyclicBarrier

/**
 * Created by zhaoyingtao
 * Date: 2021/7/1
 * Describe:测试等待所有完成在执行
 */
class CountDownLatchTestAct : BaseKotlinActivity() {
    //    CountDownLatch和CyclicBarrier区别：
//    1.countDownLatch是一个计数器，线程完成一个记录一个，计数器递减，只能只用一次
//    2.CyclicBarrier的计数器更像一个阀门，需要所有线程都到达，然后继续执行，计数器递增，提供reset功能，可以多次使用
    private var countDown: CountDownLatch? = null
    private var cyclicBarrier: CyclicBarrier? = null
    var countTime: Disposable? = null

    override fun getLayoutId(): Int {
        return R.layout.act_countdownlatch_test
    }

    override fun setViewData(savedInstanceState: Bundle?) {
//        testCountDownLatch()
//        testCyclicBarrier()

        countTime = CountTimeUtils.startCountTime(5000, 1000) {
            if (it > 10) {
                countTime?.dispose()
            }
            LogUtils.e("============" + it + "==========" + countTime?.isDisposed)
        }
//        var clipboardManager: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
//        clipboardManager.setPrimaryClip(ClipData.newPlainText("content","剪切板的内容"))
    }

    private fun testCyclicBarrier() {
        cyclicBarrier = CyclicBarrier(3) {
            LogUtils.e("三个人都已到达会议室")
        }
        ThreadCacheTools.newThread {
            Thread.sleep(1000)
            LogUtils.e("第" + Thread.currentThread().name + "个人到达会议室")
            cyclicBarrier?.await()
            LogUtils.e("第" + Thread.currentThread().name + "个人“开始”会议室")
        }
        ThreadCacheTools.newThread {
            Thread.sleep(2000)
            LogUtils.e("第" + Thread.currentThread().name + "个人到达会议室")
            cyclicBarrier?.await()
            LogUtils.e("第" + Thread.currentThread().name + "个人“开始”会议室")
        }
        ThreadCacheTools.newThread {
            Thread.sleep(3000)
            LogUtils.e("第" + Thread.currentThread().name + "个人到达会议室")
            cyclicBarrier?.await()
            LogUtils.e("第" + Thread.currentThread().name + "个人“开始”会议室")
        }
    }

    private fun testCountDownLatch() {
        countDown = CountDownLatch(2)
        LogUtils.e("主线程开始执行…… ……")
        ThreadCacheTools.newThread {
            Thread.sleep(2000)
            LogUtils.e("子线程：" + Thread.currentThread().name + "执行")
            countDown?.countDown()
        }
        ThreadCacheTools.newThread {
            Thread.sleep(4000)
            LogUtils.e("子线程：" + Thread.currentThread().name + "执行")
            countDown?.countDown()
        }
        LogUtils.e("等待两个线程执行完毕…… ……")
        countDown?.await()
        LogUtils.e("两个子线程都执行完毕，继续执行主线程")
    }
}